local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "agenthelper"
local msgproxy = require "msgproxy"
local table = table
local processstate = require "processstate"
local globaldatadao = require "globaldatadao"
local timetool = require "timetool"
local configdao = require "configdao"
require "enum"
local processing = processstate:new({timeout=4})
local  GetTableRecords = {}

--[[
//获得战绩信息的牌局记录
message GetTableRecordsReq {
	optional Version version = 1;
	optional string id = 2; //战绩记录id
	optional int32 gameid = 3;   
}

//获得战绩信息的牌局记录
message GetTableRecordsRes {
	optional int32 errcode = 1;
	optional string errcodedes = 2;
	optional int32 gameid = 3;
	repeated PhzGameResultNtc gameresults = 4;
}
]]

function  GetTableRecords.process(session, source, fd, request)
    local responsemsg = {
        errcode = EErrCode.ERR_SUCCESS,
        gameid = request.gameid
    }
    local server = msghelper:get_server()

    --检查当前登陆状态
    if not msghelper:is_login_success() then
        filelog.sys_warning("GetTableRecords.process invalid server state", server.state)
        responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
        responsemsg.errcodedes = "无效的请求！"
        msghelper:send_resmsgto_client(fd, "GetTableRecordsRes", responsemsg)
        return
    end

    if processing:is_processing() then
        responsemsg.errcode = EErrCode.ERR_DEADING_LASTREQ
        responsemsg.errcodedes = "正在处理上一次请求！"
        msghelper:send_resmsgto_client(fd, "GetTableRecordsRes", responsemsg)
        return
    end

    local status
    local gameresults

    processing:set_process_state(true)
    status, gameresults = globaldatadao.query_tablerecord(request.id, request.gameid)
    processing:set_process_state(false)
    if not msghelper:is_login_success() then
        filelog.sys_warning("GetTableRecords.process invalid server state", server.state)
        responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
        responsemsg.errcodedes = "无效的请求！"
        msghelper:send_resmsgto_client(fd, "GetTableRecordsRes", responsemsg)
        return
    end

    if status == nil or gameresults == nil  then
        responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
        responsemsg.errcodedes = "访问数据失败！"
        msghelper:send_resmsgto_client(fd, "GetTableRecordsRes", responsemsg)
        return        
    end

    if request.gameid == EGameId.GAME_ID_PENGHUZI then
        responsemsg.gameresults = gameresults        
    elseif request.gameid == EGameId.GAME_ID_NIUNIU then
        responsemsg.nngameresults = gameresults                
    end
    	
    msghelper:send_resmsgto_client(fd, "GetTableRecordsRes", responsemsg)

end

return GetTableRecords
